#include "ChatGrpcClient.h"
#include"ConFigMgr.h"

ChatGrpcClient::ChatGrpcClient() {
    auto& cfg = ConFigMgr::Inst();
    auto server_list = cfg["PeerServer"]["Servers"];

    std::vector<std::string> server_names;

    std::stringstream ss(server_list);
    std::string server_name;

    while (std::getline(ss, server_name, ','))
    {
        server_names.push_back(server_name);
    }

    for (auto& name : server_names) {
        if (cfg[name]["Name"].empty())
        {
            continue;
        }
        _pools[cfg[name]["Name"]] = std::make_unique<ChatConPool>(5, cfg[name]["Host"], cfg[name]["Port"]);
    }

}





AddFriendRsp ChatGrpcClient::NotifyAddFriend(std::string server_ip, const AddFriendReq& req)
{
    message::AddFriendRsp rsp;
    Defer defer([&rsp,&req](){
        rsp.set_applyuid(req.applyuid());
        rsp.set_error(ErrorCodes::SUCCESS);
        rsp.set_touid(req.touid());
        
        });

    auto find_iter = _pools.find(server_ip);
    if (find_iter==_pools.end()) {
        return rsp;
    }

    auto& pool = find_iter->second;
    ClientContext context;
    auto stub = pool->getConnection();
    Status status = stub->NotifyAddFriend(&context, req, &rsp);
    Defer returndefer([&pool, this, &stub]() {
        pool->returnConnection(std::move(stub));
        });

    if (!status.ok()) {
        rsp.set_error(ErrorCodes::RCP_faild);
    }

    return rsp;
}

AuthFriendRsp ChatGrpcClient::NotifyAuthFriend(std::string server_ip, const AuthFriendReq& req)
{
    AuthFriendRsp rsp;
    Defer defer([&rsp,&req]() {
        rsp.set_fromuid(req.fromuid());
        rsp.set_touid(req.touid());
        rsp.set_error(ErrorCodes::SUCCESS);
        });

    auto iter = _pools.find(server_ip);
    if (iter == _pools.end()) {
        return rsp;
    }

    auto& pool = iter->second;
    auto con = pool->getConnection();
    ClientContext context;
    Status status = con->NotifyAuthFriend(&context, req, &rsp);
    Defer reCon([&con, &pool, this]() {
        pool->returnConnection(std::move(con));
        });
    if (!status.ok()) {
        rsp.set_error(ErrorCodes::RCP_faild);
    }
    return rsp;
}

bool ChatGrpcClient::GetBaseInfo(std::string base_key, int uid, std::shared_ptr<UserInfo>& userinfo)
{
    return false;
}

TextChatMsgRsp ChatGrpcClient::NotifyTextChatMsg(std::string server_ip, const TextChatMsgReq& req)
{
    TextChatMsgRsp rsp;
    Defer defer([&req,&rsp]() {
        rsp.set_error(ErrorCodes::SUCCESS);
        rsp.set_fromuid(req.fromuid());
        rsp.set_touid(req.touid());

        for (const auto& msg : req.textmsgs()){
            TextChatData* text_data = rsp.add_textmsgs();
            text_data->set_msgcontent(msg.msgcontent());
            text_data->set_msgid(msg.msgid());
        }

        });

    auto iter = _pools.find(server_ip);
    if (iter == _pools.end()) {
        rsp.set_error(ErrorCodes::RCP_faild);
        return rsp;
    }

    auto& pool = iter->second;
    auto con = pool->getConnection();
    if (!con) {
        rsp.set_error(ErrorCodes::RCP_faild);
        return rsp;
    }
    ClientContext context;
    Status status = con->NotifyTextChatMsg(&context, req, &rsp);

    Defer defercon([&con,&pool]() {
        pool->returnConnection(std::move(con));
        });

    if (!status.ok()) {
        rsp.set_error(ErrorCodes::RCP_faild);
        return rsp;
    }
    return rsp;

}
